home *** CD-ROM | disk | FTP | other *** search
/ PC Open 102 / PC Open 102 CD 1.bin / CD1 / INTERNET / EMAIL / pop file / setup.exe / $_1_ / HTTP / Response.pm < prev    next >
Encoding:
Perl POD Document  |  2004-02-03  |  11.0 KB  |  414 lines

  1. package HTTP::Response;
  2.  
  3. # $Id: Response.pm,v 1.41 2003/10/24 10:25:16 gisle Exp $
  4.  
  5. require HTTP::Message;
  6. @ISA = qw(HTTP::Message);
  7. $VERSION = sprintf("%d.%02d", q$Revision: 1.41 $ =~ /(\d+)\.(\d+)/);
  8.  
  9. use strict;
  10. use HTTP::Status ();
  11.  
  12.  
  13.  
  14. sub new
  15. {
  16.     my($class, $rc, $msg, $header, $content) = @_;
  17.     my $self = $class->SUPER::new($header, $content);
  18.     $self->code($rc);
  19.     $self->message($msg);
  20.     $self;
  21. }
  22.  
  23.  
  24. sub clone
  25. {
  26.     my $self = shift;
  27.     my $clone = bless $self->SUPER::clone, ref($self);
  28.     $clone->code($self->code);
  29.     $clone->message($self->message);
  30.     $clone->request($self->request->clone) if $self->request;
  31.     # we don't clone previous
  32.     $clone;
  33. }
  34.  
  35.  
  36. sub code      { shift->_elem('_rc',      @_); }
  37. sub message   { shift->_elem('_msg',     @_); }
  38. sub previous  { shift->_elem('_previous',@_); }
  39. sub request   { shift->_elem('_request', @_); }
  40.  
  41.  
  42. sub status_line
  43. {
  44.     my $self = shift;
  45.     my $code = $self->{'_rc'}  || "000";
  46.     my $mess = $self->{'_msg'} || HTTP::Status::status_message($code) || "?";
  47.     return "$code $mess";
  48. }
  49.  
  50.  
  51. sub base
  52. {
  53.     my $self = shift;
  54.     my $base = $self->header('Content-Base')     ||  # used to be HTTP/1.1
  55.                $self->header('Content-Location') ||  # HTTP/1.1
  56.                $self->header('Base');                # HTTP/1.0
  57.     return $HTTP::URI_CLASS->new_abs($base, $self->request->uri);
  58.     # So yes, if $base is undef, the return value is effectively
  59.     # just a copy of $self->request->uri.
  60. }
  61.  
  62.  
  63. sub as_string
  64. {
  65.     require HTTP::Status;
  66.     my $self = shift;
  67.     my @result;
  68.     #push(@result, "---- $self ----");
  69.     my $code = $self->code;
  70.     my $status_message = HTTP::Status::status_message($code) || "Unknown code";
  71.     my $message = $self->message || "";
  72.  
  73.     my $status_line = "$code";
  74.     my $proto = $self->protocol;
  75.     $status_line = "$proto $status_line" if $proto;
  76.     $status_line .= " ($status_message)" if $status_message ne $message;
  77.     $status_line .= " $message";
  78.     push(@result, $status_line);
  79.     push(@result, $self->headers_as_string);
  80.     my $content = $self->content;
  81.     if (defined $content) {
  82.     push(@result, $content);
  83.     }
  84.     #push(@result, ("-" x 40));
  85.     join("\n", @result, "");
  86. }
  87.  
  88.  
  89. sub is_info     { HTTP::Status::is_info     (shift->{'_rc'}); }
  90. sub is_success  { HTTP::Status::is_success  (shift->{'_rc'}); }
  91. sub is_redirect { HTTP::Status::is_redirect (shift->{'_rc'}); }
  92. sub is_error    { HTTP::Status::is_error    (shift->{'_rc'}); }
  93.  
  94.  
  95. sub error_as_HTML
  96. {
  97.     my $self = shift;
  98.     my $title = 'An Error Occurred';
  99.     my $body  = $self->status_line;
  100.     return <<EOM;
  101. <HTML>
  102. <HEAD><TITLE>$title</TITLE></HEAD>
  103. <BODY>
  104. <H1>$title</H1>
  105. $body
  106. </BODY>
  107. </HTML>
  108. EOM
  109. }
  110.  
  111.  
  112. sub current_age
  113. {
  114.     my $self = shift;
  115.     # Implementation of <draft-ietf-http-v11-spec-07> section 13.2.3
  116.     # (age calculations)
  117.     my $response_time = $self->client_date;
  118.     my $date = $self->date;
  119.  
  120.     my $age = 0;
  121.     if ($response_time && $date) {
  122.     $age = $response_time - $date;  # apparent_age
  123.     $age = 0 if $age < 0;
  124.     }
  125.  
  126.     my $age_v = $self->header('Age');
  127.     if ($age_v && $age_v > $age) {
  128.     $age = $age_v;   # corrected_received_age
  129.     }
  130.  
  131.     my $request = $self->request;
  132.     if ($request) {
  133.     my $request_time = $request->date;
  134.     if ($request_time) {
  135.         # Add response_delay to age to get 'corrected_initial_age'
  136.         $age += $response_time - $request_time;
  137.     }
  138.     }
  139.     if ($response_time) {
  140.     $age += time - $response_time;
  141.     }
  142.     return $age;
  143. }
  144.  
  145.  
  146. sub freshness_lifetime
  147. {
  148.     my $self = shift;
  149.  
  150.     # First look for the Cache-Control: max-age=n header
  151.     my @cc = $self->header('Cache-Control');
  152.     if (@cc) {
  153.     my $cc;
  154.     for $cc (@cc) {
  155.         my $cc_dir;
  156.         for $cc_dir (split(/\s*,\s*/, $cc)) {
  157.         if ($cc_dir =~ /max-age\s*=\s*(\d+)/i) {
  158.             return $1;
  159.         }
  160.         }
  161.     }
  162.     }
  163.  
  164.     # Next possibility is to look at the "Expires" header
  165.     my $date = $self->date || $self->client_date || time;      
  166.     my $expires = $self->expires;
  167.     unless ($expires) {
  168.     # Must apply heuristic expiration
  169.     my $last_modified = $self->last_modified;
  170.     if ($last_modified) {
  171.         my $h_exp = ($date - $last_modified) * 0.10;  # 10% since last-mod
  172.         if ($h_exp < 60) {
  173.         return 60;  # minimum
  174.         }
  175.         elsif ($h_exp > 24 * 3600) {
  176.         # Should give a warning if more than 24 hours according to
  177.         # <draft-ietf-http-v11-spec-07> section 13.2.4, but I don't
  178.         # know how to do it from this function interface, so I just
  179.         # make this the maximum value.
  180.         return 24 * 3600;
  181.         }
  182.         return $h_exp;
  183.     }
  184.     else {
  185.         return 3600;  # 1 hour is fallback when all else fails
  186.     }
  187.     }
  188.     return $expires - $date;
  189. }
  190.  
  191.  
  192. sub is_fresh
  193. {
  194.     my $self = shift;
  195.     $self->freshness_lifetime > $self->current_age;
  196. }
  197.  
  198.  
  199. sub fresh_until
  200. {
  201.     my $self = shift;
  202.     return $self->freshness_lifetime - $self->current_age + time;
  203. }
  204.  
  205. 1;
  206.  
  207.  
  208. __END__
  209.  
  210. =head1 NAME
  211.  
  212. HTTP::Response - HTTP style response message
  213.  
  214. =head1 SYNOPSIS
  215.  
  216. Response objects are returned by the request() method of the C<LWP::UserAgent>:
  217.  
  218.     # ...
  219.     $response = $ua->request($request)
  220.     if ($response->is_success) {
  221.         print $response->content;
  222.     }
  223.     else {
  224.         print STDERR $response->status_line, "\n";
  225.     }
  226.  
  227. =head1 DESCRIPTION
  228.  
  229. The C<HTTP::Response> class encapsulates HTTP style responses.  A
  230. response consists of a response line, some headers, and a content
  231. body. Note that the LWP library uses HTTP style responses even for
  232. non-HTTP protocol schemes.  Instances of this class are usually
  233. created and returned by the request() method of an C<LWP::UserAgent>
  234. object.
  235.  
  236. C<HTTP::Response> is a subclass of C<HTTP::Message> and therefore
  237. inherits its methods.  The following additional methods are available:
  238.  
  239. =over 4
  240.  
  241. =item $r = HTTP::Response->new( $code )
  242.  
  243. =item $r = HTTP::Response->new( $code, $msg )
  244.  
  245. =item $r = HTTP::Response->new( $code, $msg, $header )
  246.  
  247. =item $r = HTTP::Response->new( $code, $msg, $header, $content )
  248.  
  249. Constructs a new C<HTTP::Response> object describing a response with
  250. response code $code and optional message $msg.  The optional $header
  251. argument should be a reference to an C<HTTP::Headers> object.  The
  252. optional $content argument should be a string of bytes.  The meaning
  253. these arguments are described below.
  254.  
  255. =item $r->code
  256.  
  257. =item $r->code( $code )
  258.  
  259. This is used to get/set the code attribute.  The code is a 3 digit
  260. number that encode the overall outcome of a HTTP response.  The
  261. C<HTTP::Status> module provide constants that provide mnemonic names
  262. for the code attribute.
  263.  
  264. =item $r->message
  265.  
  266. =item $r->message( $message )
  267.  
  268. This is used to get/set the message attribute.  The message is a short
  269. human readable single line string that explains the response code.
  270.  
  271. =item $r->header( $field )
  272.  
  273. =item $r->header( $field => $value )
  274.  
  275. This is used to get/set header values and it is inherited from
  276. C<HTTP::Headers> via C<HTTP::Message>.  See L<HTTP::Headers> for
  277. details and other similar methods that can be used to access the
  278. headers.
  279.  
  280. =item $r->content
  281.  
  282. =item $r->content( $content )
  283.  
  284. This is used to get/set the content and it is inherited from the
  285. C<HTTP::Message> base class.  See L<HTTP::Message> for details and
  286. other methods that can be used to access the content.
  287.  
  288. =item $r->request
  289.  
  290. =item $r->request( $request )
  291.  
  292. This is used to get/set the request attribute.  The request attribute
  293. is a reference to the the request that caused this response.  It does
  294. not have to be the same request passed to the $ua->request() method,
  295. because there might have been redirects and authorization retries in
  296. between.
  297.  
  298. =item $r->previous
  299.  
  300. =item $r->previous( $response )
  301.  
  302. This is used to get/set the previous attribute.  The previous
  303. attribute is used to link together chains of responses.  You get
  304. chains of responses if the first response is redirect or unauthorized.
  305. The value is C<undef> if this is the first response in a chain.
  306.  
  307. =item $r->status_line
  308.  
  309. Returns the string "E<lt>code> E<lt>message>".  If the message attribute
  310. is not set then the official name of E<lt>code> (see L<HTTP::Status>)
  311. is substituted.
  312.  
  313. =item $r->base
  314.  
  315. Returns the base URI for this response.  The return value will be a
  316. reference to a URI object.
  317.  
  318. The base URI is obtained from one the following sources (in priority
  319. order):
  320.  
  321. =over 4
  322.  
  323. =item 1.
  324.  
  325. Embedded in the document content, for instance <BASE HREF="...">
  326. in HTML documents.
  327.  
  328. =item 2.
  329.  
  330. A "Content-Base:" or a "Content-Location:" header in the response.
  331.  
  332. For backwards compatability with older HTTP implementations we will
  333. also look for the "Base:" header.
  334.  
  335. =item 3.
  336.  
  337. The URI used to request this response. This might not be the original
  338. URI that was passed to $ua->request() method, because we might have
  339. received some redirect responses first.
  340.  
  341. =back
  342.  
  343. When the LWP protocol modules produce the HTTP::Response object, then
  344. any base URI embedded in the document (step 1) will already have
  345. initialized the "Content-Base:" header. This means that this method
  346. only performs the last 2 steps (the content is not always available
  347. either).
  348.  
  349. =item $r->as_string
  350.  
  351. Returns a textual representation of the response.  Mainly
  352. useful for debugging purposes. It takes no arguments.
  353.  
  354. =item $r->is_info
  355.  
  356. =item $r->is_success
  357.  
  358. =item $r->is_redirect
  359.  
  360. =item $r->is_error
  361.  
  362. These methods indicate if the response was informational, sucessful, a
  363. redirection, or an error.  See L<HTTP::Status> for the meaning of these.
  364.  
  365. =item $r->error_as_HTML
  366.  
  367. Returns a string containing a complete HTML document indicating what
  368. error occurred.  This method should only be called when $r->is_error
  369. is TRUE.
  370.  
  371. =item $r->current_age
  372.  
  373. Calculates the "current age" of the response as specified by RFC 2616
  374. section 13.2.3.  The age of a response is the time since it was sent
  375. by the origin server.  The returned value is a number representing the
  376. age in seconds.
  377.  
  378. =item $r->freshness_lifetime
  379.  
  380. Calculates the "freshness lifetime" of the response as specified by
  381. RFC 2616 section 13.2.4.  The "freshness lifetime" is the length of
  382. time between the generation of a response and its expiration time.
  383. The returned value is a number representing the freshness lifetime in
  384. seconds.
  385.  
  386. If the response does not contain an "Expires" or a "Cache-Control"
  387. header, then this function will apply some simple heuristic based on
  388. 'Last-Modified' to determine a suitable lifetime.
  389.  
  390. =item $r->is_fresh
  391.  
  392. Returns TRUE if the response is fresh, based on the values of
  393. freshness_lifetime() and current_age().  If the response is no longer
  394. fresh, then it has to be refetched or revalidated by the origin
  395. server.
  396.  
  397. =item $r->fresh_until
  398.  
  399. Returns the time when this entiy is no longer fresh.
  400.  
  401. =back
  402.  
  403. =head1 SEE ALSO
  404.  
  405. L<HTTP::Headers>, L<HTTP::Message>, L<HTTP::Status>, L<HTTP::Request>
  406.  
  407. =head1 COPYRIGHT
  408.  
  409. Copyright 1995-2001 Gisle Aas.
  410.  
  411. This library is free software; you can redistribute it and/or
  412. modify it under the same terms as Perl itself.
  413.  
  414.